﻿using System.IO;
using Monti.MVVM;

namespace Peter.Logic
{
   /// <summary>
   /// Represents a file or directory item in the file view.
   /// </summary>
   public class FileItemViewModel : TreeViewItemViewModel
   {
      /// <summary>
      /// Initializes a new file item view model.
      /// </summary>
      /// <param name="parent">The parent item.</param>
      /// <param name="fullPath">The full path of the file item.</param>
      /// <param name="text">The text to display for the file item.</param>
      /// <param name="isDirectory">True if item is directory, otherwise false.</param>
      public FileItemViewModel (TreeViewItemViewModel parent, string fullPath, string text,
         bool isDirectory)
         : base (parent, isDirectory)
      {
         this.Text = text;
         this.FullPath = fullPath;
         this.IsDirectory = isDirectory;
         if (!isDirectory)
         {
            // TODO: LOAD ICON ON THREAD...
         }
      }

      /// <summary>
      /// Gets if this item is a directory or not.
      /// </summary>
      public bool IsDirectory { get; private set; }

      /// <summary>
      /// Gets the full path of this item.
      /// </summary>
      public string FullPath { get; private set; }

      /// <summary>
      /// Loads any children nodes.
      /// </summary>
      protected override void LoadChildren ()
      {
         if (!this.IsDirectory) return;

         var dirInfo = new DirectoryInfo (this.FullPath);
         var directories = dirInfo.GetDirectories ();
         var files = dirInfo.GetFiles ();

         foreach (var directory in directories)
            this.Children.Add (new FileItemViewModel (this, directory.FullName, directory.Name, true));

         foreach (var file in files)
            this.Children.Add (new FileItemViewModel (this, file.FullName, file.Name, false));
      }
   }
}
